package o;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.text.TextUtils;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appmarket.service.alarm.control.RepeatingTaskManager;
import com.huawei.appmarket.service.reserve.game.bean.GetOrderedAppsRes;
import com.huawei.appmarket.service.reserve.game.bean.OrderAppInfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class dug {
    private static boolean ˏ = false;

    public static boolean ˋ() {
        return ˏ;
    }

    public static void ˊ(boolean z) {
        ˏ = z;
    }

    public static boolean ˎ(String str, int i) {
        ApkUpgradeInfo ॱ = ॱ(str);
        return ॱ != null && ॱ.ʿ() == i;
    }

    public static ApkUpgradeInfo ॱ(String str) {
        ApkUpgradeInfo ˊ = ((cdg) cdb.ॱ(cdg.class)).ˊ(str, false, 0);
        return ˊ != null ? ˊ : ((cdg) cdb.ॱ(cdg.class)).ˋ(str, false, 0);
    }

    public static void ˊ(final Context context) {
        Logger.ˎ("AppUpgradeManager", "getOnlineUpgradeAppData");
        dsi.ˏ(new AsyncTask() {
            protected Object doInBackground(Object[] objArr) {
                euc.ˊ().ॱ();
                int ˎ = dug.ˎ(true);
                euc.ˊ().ˏ();
                if (ˎ == 0) {
                    Intent intent = new Intent();
                    intent.setPackage(context.getPackageName());
                    intent.setAction(dvq.ˊ());
                    context.sendBroadcast(intent);
                }
                return null;
            }
        });
    }

    public static int ˎ(boolean z) {
        int i = 0;
        czo ॱ = dss.ॱ().ॱ(ContentManager.getInstant().getContext(), z, 0);
        if (ॱ.ˎ() == 0 && ॱ.ˋ() == 0) {
            if (UserSession.getInstance().isLoginSuccessful()) {
                if (!CollectCheck.isEmpty(efg.ˋ().ॱ())) {
                    Logger.ˎ("AppUpgradeManager", "start to get reserved game from store");
                    ˎ();
                }
                i = eny.ˊ().ʻ();
            }
            ˎ(ContentManager.getInstant().getContext(), i);
        }
        if (ॱ.ˎ() != 0) {
            return ॱ.ˎ();
        }
        return ॱ.ˋ();
    }

    private static void ˎ() {
        ResponseBean ˊ = efb.ॱ().ˊ();
        if (ˊ.getResponseCode() == 0 && ˊ.getRtnCode_() == 0) {
            List ˋ = ((GetOrderedAppsRes) ˊ).ˋ();
            efg.ˋ().ˏ(ˋ);
            Collection arrayList = new ArrayList();
            ˋ(ˋ, arrayList);
            Logger.ˎ("AppUpgradeManager", "size of gameOnShelf： " + arrayList.size());
            ((cdf) cdb.ॱ(cdf.class)).ˊ(arrayList);
        }
    }

    private static void ˋ(List<OrderAppInfo> list, List<ApkUpgradeInfo> list2) {
        if (CollectCheck.isEmpty((List) list)) {
            Logger.ˎ("AppUpgradeManager", "orderAppInfoList is empty");
            return;
        }
        for (OrderAppInfo orderAppInfo : list) {
            if (!TextUtils.isEmpty(orderAppInfo.ॱˊ())) {
                int parseInt;
                ApkUpgradeInfo apkUpgradeInfo;
                try {
                    parseInt = Integer.parseInt(orderAppInfo.ʻॱ());
                } catch (NumberFormatException e) {
                    Logger.ˎ("AppUpgradeManager", "versionCode NumberFormatException error:" + e.getMessage());
                    parseInt = 0;
                }
                if (!((cdg) cdb.ॱ(cdg.class)).ॱॱ(orderAppInfo.ˎ())) {
                    apkUpgradeInfo = new ApkUpgradeInfo();
                    apkUpgradeInfo.ˎ(true);
                    apkUpgradeInfo.ʼ(orderAppInfo.ʻ());
                    apkUpgradeInfo.ˊ(orderAppInfo.ॱ());
                    apkUpgradeInfo.ʽ(orderAppInfo.ॱˊ());
                    apkUpgradeInfo.ˋ(orderAppInfo.ˋॱ());
                    apkUpgradeInfo.ॱ(orderAppInfo.ͺ());
                    apkUpgradeInfo.ˋ(orderAppInfo.ˎ());
                    apkUpgradeInfo.ᐝ(orderAppInfo.ॱᐝ());
                    apkUpgradeInfo.ˏ(parseInt);
                    apkUpgradeInfo.ˏ(orderAppInfo.ॱॱ());
                    apkUpgradeInfo.ˎ(orderAppInfo.ᐝॱ());
                    apkUpgradeInfo.ॱ(orderAppInfo.ॱˎ());
                    apkUpgradeInfo.ॱॱ(orderAppInfo.ॱˊ());
                    apkUpgradeInfo.ˋ(orderAppInfo.ˊ());
                } else if (((cdg) cdb.ॱ(cdg.class)).ˎ(orderAppInfo.ˎ(), false, 0)) {
                    Logger.ˎ("AppUpgradeManager", "Reserved Game in NotRecomUpdate , abort it");
                } else {
                    ApkUpgradeInfo ˊ = ((cdg) cdb.ॱ(cdg.class)).ˊ(orderAppInfo.ˎ(), false, 0);
                    PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(orderAppInfo.ˎ());
                    if (ᐝ != null && ᐝ.versionCode >= parseInt) {
                        Logger.ˎ("AppUpgradeManager", "installedApp versioncode is larger , abort it");
                    } else if (ˊ == null) {
                        Logger.ˎ("AppUpgradeManager", "reserved game on shelf but cannot get upgrade info");
                    } else if (ˊ.ˏॱ() == 1 || ˊ.ॱˋ() == 1) {
                        Logger.ˎ("AppUpgradeManager", "different signature or lower target sdk , abort it");
                    } else {
                        apkUpgradeInfo = ॱ(ˊ);
                        apkUpgradeInfo.ˋ(orderAppInfo.ˊ());
                    }
                }
                ॱ(apkUpgradeInfo, list2, orderAppInfo.ˏ(), orderAppInfo.ˋ());
            }
        }
    }

    private static ApkUpgradeInfo ॱ(ApkUpgradeInfo apkUpgradeInfo) {
        ApkUpgradeInfo apkUpgradeInfo2;
        IllegalAccessException e;
        JSONException e2;
        ClassNotFoundException e3;
        InstantiationException e4;
        try {
            JSONObject jSONObject = new JSONObject(apkUpgradeInfo.toJson());
            apkUpgradeInfo2 = new ApkUpgradeInfo();
            try {
                apkUpgradeInfo2.fromJson(jSONObject);
                apkUpgradeInfo2.ˎ(true);
            } catch (IllegalAccessException e5) {
                e = e5;
                Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e.getMessage());
                return apkUpgradeInfo2;
            } catch (JSONException e6) {
                e2 = e6;
                Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e2.getMessage());
                return apkUpgradeInfo2;
            } catch (ClassNotFoundException e7) {
                e3 = e7;
                Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e3.getMessage());
                return apkUpgradeInfo2;
            } catch (InstantiationException e8) {
                e4 = e8;
                Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e4.getMessage());
                return apkUpgradeInfo2;
            }
        } catch (IllegalAccessException e9) {
            IllegalAccessException illegalAccessException = e9;
            apkUpgradeInfo2 = null;
            e = illegalAccessException;
            Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e.getMessage());
            return apkUpgradeInfo2;
        } catch (JSONException e10) {
            JSONException jSONException = e10;
            apkUpgradeInfo2 = null;
            e2 = jSONException;
            Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e2.getMessage());
            return apkUpgradeInfo2;
        } catch (ClassNotFoundException e11) {
            ClassNotFoundException classNotFoundException = e11;
            apkUpgradeInfo2 = null;
            e3 = classNotFoundException;
            Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e3.getMessage());
            return apkUpgradeInfo2;
        } catch (InstantiationException e12) {
            InstantiationException instantiationException = e12;
            apkUpgradeInfo2 = null;
            e4 = instantiationException;
            Logger.e("AppUpgradeManager", "ApkUpgradeInfo Clone error:" + e4.getMessage());
            return apkUpgradeInfo2;
        }
        return apkUpgradeInfo2;
    }

    private static void ॱ(ApkUpgradeInfo apkUpgradeInfo, List<ApkUpgradeInfo> list, String str, String str2) {
        if (apkUpgradeInfo != null) {
            if (apkUpgradeInfo.ˎ() == 1) {
                list.add(apkUpgradeInfo);
                Logger.ˎ("AppUpgradeManager", "gameOnShelf: " + apkUpgradeInfo.ˊ());
            } else {
                Logger.ˎ("AppUpgradeManager", "gameOnShelf but no need to start download: " + apkUpgradeInfo.ˊ());
            }
            efa.ˏ(ContentManager.getInstant().getContext(), apkUpgradeInfo, str, str2);
        }
    }

    private static void ˎ(Context context, int i) {
        boolean z = false;
        List ᐝॱ = dss.ॱ().ᐝॱ();
        Collection ˊ = dss.ॱ().ˊ(false, 0);
        Collection ˏ = dss.ॱ().ˏ(false, 0);
        if (!(ᐝॱ.containsAll(ˊ) && ᐝॱ.containsAll(ˏ))) {
            z = true;
        }
        ᐝॱ = new ArrayList();
        if (z) {
            ᐝॱ.add(dpv.class);
        }
        if (((cdg) cdb.ॱ(cdg.class)).ˊ() > 0) {
            ᐝॱ.add(dqd.class);
        }
        if (i > 0) {
            ᐝॱ.add(dqs.class);
        }
        if (ᐝॱ.size() > 0) {
            RepeatingTaskManager.execute(context, (Class[]) ᐝॱ.toArray(new Class[ᐝॱ.size()]));
        }
    }
}
